package com.wangp.mywrite.coding;

import java.util.*;

/**
 * @author tooStronger
 * @date 2023/2/28
 */
public class MergeSimilarItems {

    public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int[] ints : items1) {
            map.put(ints[0], ints[1]);
        }
        for (int[] ints : items2) {
            map.put(ints[0], map.getOrDefault(ints[0], 0) + ints[1]);
        }
        List<List<Integer>> res = new ArrayList<>();
        map.forEach((k, v) -> {
            res.add(Arrays.asList(k, v));
        });

         res.sort(Comparator.comparingInt(o -> o.get(0)));
         return res;
    }

    public static void main(String[] args) {
        System.out.println(new MergeSimilarItems().mergeSimilarItems(new int[][]{{15,5},{2,6},{5,3},{14,8},{12,4},{19,6},{25,4},{13,4},{9,1}}, new int[][]{{15,9},{2,4},{5,2},{14,4},{12,3},{19,10},{25,7},{13,6},{9,9}}));
    }
}
